package net.alpcontrol.bi.bootstrap.utils;

import cn.hutool.core.util.ClassUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;

public class AlpLogUtils
{
  private static final Logger logger = LoggerFactory.getLogger(AlpLogUtils.class);

  public static String getName()
  {
    return logger.getName();
  }

  public static boolean isTraceEnabled() {
    return logger.isTraceEnabled();
  }

  public static void trace(String string) {
    logger.trace(string);
  }

  public static boolean isDebugEnabled() {
    return logger.isDebugEnabled();
  }

  public static void debug(String string) {
    logger.debug(string);
  }

  public static boolean isInfoEnabled() {
    return logger.isInfoEnabled();
  }

  public static void info(String string) {
    logger.info(string);
  }

  public static boolean isWarnEnabled() {
    return logger.isWarnEnabled();
  }

  public static void warn(String string) {
    logger.warn(string);
  }

  public static boolean isErrorEnabled() {
    return logger.isErrorEnabled();
  }

  public static void error(String string) {
    logger.error(string);
  }


  public static void error(String message,Object... params) {
    print(Level.ERROR, message, params);
  }

  public static void warn(String message,Object... params) {
    print(Level.WARN, message, params);
  }

  public static void info(String message,Object... params) {
    print(Level.INFO, message, params);
  }
  public static void debug(String message, Object... params) {
    print(Level.DEBUG, message, params);
  }

  public static void trace(String message,Object... params) {
    print(Level.TRACE, message, params);
  }

  private static void print(Level level, String message, Object... params) {
    String[] paramArr = toJsonStringArr(params);
    String printMessage = buildMessage(message, paramArr);
    switch (level) {
      case ERROR:
        logger.error(printMessage,paramArr);
        return;
      case WARN:
        logger.warn(printMessage, paramArr);
        return;
      case DEBUG:
        logger.debug(printMessage,paramArr);
        return;
      case INFO:
        logger.info(printMessage, paramArr);
        return;
      case TRACE:
        logger.trace(printMessage, paramArr);
        return;
    }
  }

  private static String buildMessage(String message, String[] paramArr) {
    StringBuilder stringBuilder = new StringBuilder(message);
    if (message.indexOf("{") < 0) {
      stringBuilder.append(":\n");
      for (int i = 0; i < paramArr.length - 1; i++) {
        stringBuilder.append("{}, ");
      }
      stringBuilder.append("{}");
    }
    return stringBuilder.toString();
  }

  private static String[] toJsonStringArr(Object... params) {
    int length = params == null ? 0 : params.length;
    String[] paramArr = new String[length];
    for (int i = 0; i < length; i++) {
      Object param = params[i];
      if(ClassUtil.isBasicType(param.getClass())) {
        paramArr[i] = String.valueOf(param);
      } else {
        paramArr[i] = JacksonUtil.toJsonString(param);
      }
    }
    return paramArr;
  }
}




